package api.extractExpert;

import base.response.OffsetLimitPage;
import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import common.BaseController;
import common.CommonResult;
import common.validateUser.InterfaceUsers;
import common.validateUser.ValidateUserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.dictionary.ItemTypeServices;
import services.dictionary.MajorTypeServices;
import services.specific.ExtractExpertServices;
import services.staff.StaffInfoServices;
import util.DateUtil;
import validator.NotBlankFluentValidator;
import validator.NotNullFluentValidator;
import wt.entity.business.BusinessInfo;
import wt.entity.specific.ExtractExpert;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/8/16.
 */
@Controller
@RequestMapping("httpApi")
public class ManuallyExtractionExpertController extends BaseController {
    @Autowired
    public InterfaceUsers interfaceUsers;
    @Autowired
    ItemTypeServices itemTypeServices;
    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    StaffInfoServices staffInfoServices;
    @Autowired
    MajorTypeServices majorTypeServices;
    @Autowired
    ExtractExpertServices extractExpertServices;

    /**
     * 手动抽取
     *
     * @param data
     * @param uid
     * @param sign
     * @return
     */
    @ResponseBody
    @RequestMapping(value = {"gongXu/manuallyExtractionExpert","jianYan/manuallyExtractionExpert"}, produces = "application/json;charset=UTF-8")
    public CommonResult manuallyExtractionExpert(
            String uid,
            Long timeStamp,
            String sign,
            String businessCode,
            String name,
            Short outLander,
            String affiliateCompany,
            Integer offset,
            Integer limit,
            String majorNumList,
            String gradeList,
            @DateTimeFormat(pattern = DateUtil.DATE_PATTEN) Date startTime,
            @DateTimeFormat(pattern = DateUtil.DATE_PATTEN) Date endTime
    ) throws Exception {


        ComplexResult complexResult = FluentValidator.checkAll().failOver()
                .on(uid, new NotBlankFluentValidator("uid"))
                .on(timeStamp, new NotNullFluentValidator("timeStamp"))
                .on(sign, new NotBlankFluentValidator("sign"))
                .on(businessCode, new NotBlankFluentValidator("businessCode"))
                .on(limit, new NotNullFluentValidator("limit"))
                .on(startTime, new NotNullFluentValidator("startTime"))
                .on(endTime, new NotNullFluentValidator("endTime"))
                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!complexResult.isSuccess()) {
            CommonResult commonResult = new CommonResult();
            commonResult.addMessage(complexResult);
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }

        CommonResult commonResult = ValidateUserUtil.validateUser(uid, timeStamp, sign, interfaceUsers);  //用户验证
        if (!CommonResult.ZJ001.equals(commonResult.getCode())) {
            return commonResult;
        }


        BusinessInfo businessInfo = businessInfoServices.selectByBusinessCode(businessCode);

        if (businessInfo == null) {
            commonResult.setMessage(CommonResult.ZJ005_M + "businessCode");
            commonResult.setCode(CommonResult.ZJ005);
            return commonResult;
        }

        PageHelper.offsetPage(offset, limit);
        List<String> majorNumListHandle = (StringUtils.isBlank(majorNumList) ? null : Arrays.asList(majorNumList.trim().split(",")));
        List<String> gradeListHandle = (StringUtils.isBlank(gradeList) ? null : Arrays.asList(gradeList.trim().split(",")));
        List<ExtractExpert> extractExpertList = extractExpertServices.selectManuallyExtractExpert(name, outLander, gradeListHandle, majorNumListHandle, businessInfo.getItemId(),affiliateCompany, startTime, endTime);
        OffsetLimitPage offsetLimitPage = new OffsetLimitPage();
        offsetLimitPage.setTotal(((Page<ExtractExpert>) extractExpertList).getTotal());
        offsetLimitPage.setRows(HandleResult.treatmentResult(extractExpertList));

        commonResult.setData(offsetLimitPage);
        commonResult.setMessage(CommonResult.ZJ001_M);
        commonResult.setCode(CommonResult.ZJ001);
        return commonResult;
    }

}
